#!/bin/bash

# Slackware build script for pcem-roms

# K. Eugene Carlson (kvngncrlsn@gmail.com)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=pcem-roms
SRCNAM=PCem-ROMs
VERSION=${VERSION:-17.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
ARCH=noarch
PKGTYPE=${PKGTYPE:-tgz}

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.gz
cd $SRCNAM-$VERSION
# A missing rom for the Commodore PC-10
mkdir cbm_pc10
cp $CWD/cbm-pc10c-bios-v4.41-318085-08.bin cbm_pc10

chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

mkdir -p $PKG/usr/share/pcem/roms
cp -ar ./* $PKG/usr/share/pcem/roms

cd $PKG/usr/share/pcem/roms

# Extra files for the mach64g (mostly Windows 3.1/95 drivers and utilities)
# were included in the archive at the time of release, but have since been
# removed. These will not be installed by default. To install them anyway, set
# EXTRAS=yes.
if [ ! ${EXTRAS:-no} = yes ]; then
  rm -r mach64g/Drivers\ \&\ Utils
fi

# Getting rid of extraneous text files.
find . -name roms.txt -delete

# Other cleanup
rm -r kmxc02/3ctm005\ \(broken\).bin ne1000/url.txt xtide/serdrive.exe \
      pb_1300sx pb_l300sx/pb_l300sx.zip gd5434 gd5430/pci.bin1 \
      europc/50146\ char\ d1.0\ euro.u16 super286tr/hyundai_award286.bin \
      amic206.bin t1000/t1000font.bin vs440fx/BIOSCS1.REC \
      mach64g/bios.bin pb570/gd5430.bin1 infowaym/gd5434.bin

# Capitalization fixes
mv bull_micral_45/EVEN.FIL bull_micral_45/even.fil
mv bull_micral_45/ODD.FIL bull_micral_45/odd.fil
mv spc4200p/U8.01 spc4200p/u8.01
mv spc4620p/31005H.U8 spc4620p/31005h.u8
mv spc4620p/31005H.U10 spc4620p/31005h.u10
mv spc6033p/phoenix.BIN spc6033p/phoenix.bin
mv pb410a/pb410a.080337.4abf.u25.bin pb410a/PB410A.080337.4ABF.U25.bin
mv zappa/1006BS0_.BIO zappa/1006bs0_.bio
mv zappa/1006BS0_.BI1 zappa/1006bs0_.bi1
mv pb520r/1009BC0R.BIO pb520r/1009bc0r.bio
mv pb520r/1009BC0R.BI1 pb520r/1009bc0r.bi1
mv pb570/1007BY0R.BIO pb570/1007by0r.bio
mv pb570/1007BY0R.BI1 pb570/1007by0r.bi1
mv p55tvp4/TV5I0204.AWD p55tvp4/tv5i0204.awd
mv p55t2p4/0207_J2.BIN p55t2p4/0207_j2.bin
mv oti037/BIOS.BIN oti037/bios.bin
mv BlasterPCI.rom blasterpci.rom
mv Pci_sg.rom pci_sg.rom

cd $TMP/$SRCNAM-$VERSION

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
cat $CWD/README > $PKG/usr/doc/$PRGNAM-$VERSION/README.sw
mv $PKG/usr/share/pcem/roms/{CONTRIBUTING.md,README.md} \
  $PKG/usr/doc/$PRGNAM-$VERSION
# Well, at least one part of this collection is licensed!
mv $PKG/usr/share/pcem/roms/xtide/license.txt $PKG/usr/doc/$PRGNAM-$VERSION/xtide-license.txt
mv $PKG/usr/share/pcem/roms/xtide/readme.txt $PKG/usr/doc/$PRGNAM-$VERSION/xtide-readme.txt

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
